package com.cqc.jdk8.Jutil.stream;

import com.cqc.eb.UserEB;
import org.junit.Before;
import org.junit.Test;

import java.util.*;
import java.util.concurrent.TimeUnit;

public class StreamTest {

    List<String> stringList = null;
    List<String> list = null;
    List<UserEB> userEBList1 = null;

    @Before
    public void before() {
        //串行 并行
        stringList = getStringList();
        list = getStringList2();
        userEBList1 = getUserList1();
    }




    @Test
    public void parallelStream() {
        //并行 parallelStream()
        long t1 = System.nanoTime();
        long count = list.parallelStream()
                .sorted()
                .count();
        System.out.println(count);
        long t2 = System.nanoTime();
        long t = TimeUnit.NANOSECONDS.toMillis(t2 - t1);
        System.out.println(String.format("millis is %d ms", t));
    }

    @Test
    public void stream() {
        //串行排序  stream() 单个线程
        long t1 = System.nanoTime();
        long count = list.stream()
                .sorted()
                .count();
        System.out.println(count);
        long t2 = System.nanoTime();
        long t3 = TimeUnit.NANOSECONDS.toMillis(t2 - t1);
        System.out.println(String.format("time is %d ms", t3));
        //TimeUnit: https://blog.csdn.net/lovelichao12/article/details/76080888
        long l = TimeUnit.NANOSECONDS.toSeconds(t2 - t1);
        System.out.println(String.format("time is %d s", l));
    }

    @Test
    public void reduce() {
        //reduce
        Optional<String> reduce = stringList.stream()
                .sorted()
                .reduce((a, b) -> a + "#" + b);
        reduce.ifPresent(System.out::println);
        stringList.stream()
                .sorted()
                .reduce((a, b) -> a + "#" + b)
                .ifPresent(System.out::println);

        String s = stringList.stream()
                .sorted()
                .reduce((a, b) -> a + "#" + b)
                .get();

    }


    @Test
    public void count() {
        //count
        long a = stringList.stream()
                .filter((s) -> s.startsWith("a"))
                .count();
        System.out.println(a);
    }









    private static List<String> getStringList() {
        List<String> stringCollection = new ArrayList<>();
        stringCollection.add("ddd2");
        stringCollection.add("aaa2");
        stringCollection.add("bbb1");
        stringCollection.add("aaa1");
        stringCollection.add("bbb3");
        stringCollection.add("ccc1");
        stringCollection.add("bbb2");
        stringCollection.add("ddd1");
        return stringCollection;
    }


    private static List<String> getStringList2() {
        List<String> list = new ArrayList<>();
        for (int i = 0; i < 5000000; i++) {
            list.add(UUID.randomUUID().toString());
        }
        return list;
    }

    private static List<UserEB> getUserList1() {
        List<UserEB> list = new ArrayList<>();
        UserEB w = new UserEB("w", 10);
        UserEB x = new UserEB("x", 11);
        UserEB y = new UserEB("y", 12);
        list.add(w);
        list.add(x);
        list.add(y);
        return list;
    }
}
